home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6688 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.4 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.ada,comp.lang.c++,comp.lang.c,comp.lang.modula3,comp.lang.modula2
  4. Subject: Re: Hungarian notation - whoops!
  5. Date: Mon, 12 Feb 96 16:47:04 GMT
  6. Organization: none
  7. Message-ID: <824143624snz@genesis.demon.co.uk>
  8. References: <4fms62$c0p@goanna.cs.rmit.EDU.AU>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4fms62$c0p@goanna.cs.rmit.EDU.AU>
  15.            ok@goanna.cs.rmit.EDU.AU "Richard A. O'Keefe" writes:
  16.  
  17. >I have done a paper design for a machine, but never a real machine.
  18. >Sign and magnitude makes multiplication and division easy (just XOR the
  19. >signs and operate on the absolute values).  Comparison isn't any harder
  20. >than 2s magnitude.  If you can do addition and subtraction with 2s
  21. >complement, you can certainly do it for sign and magnitude with only a
  22. >couple more gate delays.
  23.  
  24. Sure it is not difficult to implement sign-magnitude integer operations
  25. but you would have to add extra CPU instructions in order to do so.
  26. 2's complement has the big advantage that it can be done using the same
  27. instructions in many cases as unsigned arithmetic. This is particular
  28. relevant to RISC archetectures where opcode space is valued for other
  29. purposes such as addressing more registers.
  30.  
  31. ...
  32.  
  33. >The underlying base is not something I usually have to think about.
  34. >Most of my C code would work just as well in base 2, 3, 10, or even
  35. >balanced ternary.  But the fact that abs(x) may deliver a negative
  36. >number is something I have to live with the whole time.  _That_ is
  37. >what I regard as silly.  I don't particularly care for the fact that
  38. >there are representable numbers X, Y for which X % Y may overflow,
  39. >either.
  40.  
  41. This is a non-issue. If you treat any expression resulting in 'the most
  42. negative 2's complement integer' as an illegal result as far as your
  43. code is concerned then you are back to the same situation as if you
  44. were using sign-magnitude. Consider:
  45.  
  46.     int x = -32767-1;
  47.     int y = abs(x);
  48.  
  49. in:
  50.  
  51. 1. A 2's complement implementation with INT_MAX==32767 and INT_MIN==-32768
  52.  
  53. x is set to -32768
  54. the 2nd expression results in undefined behaviour.
  55.  
  56. 2. A sign-magnitude implementation with INT_MAX==32767 and INT_MIN==-32767
  57.  
  58. the first expression results in undefined behaviour.
  59.  
  60. So as you can see the overall effect is the same in both cases. The only
  61. issue I can see is whether in a particular case you'd want to use
  62. INT_MIN or -INT_MAX for bounds checking.
  63.  
  64. I am aware of the rather wide distribution of this follow-up. However the
  65. issues here are applicable to other languages - I hope the C syntax
  66. is self-evident! :-)
  67.  
  68.   As far as I can see, the major "architectural
  69. >reason" these days is "compatibility with existing C".  (A little
  70. >unfair, but not a lot unfair.)
  71.  
  72. See above.
  73.  
  74. >I think that Dijkstra was right when he said "the purpose of machines is
  75. >to execute our programs", and I call anything "silly" that forces me to
  76. >work around arithmetic nasties in my source code.
  77.  
  78. Do you check every signed integer operation for overflow (barring the ones
  79. you can prove can't overflow)?
  80.  
  81. Whether the answer is yes or no you are no worse off using 2's complement
  82. that sign-magnitude (but for different reasons).
  83.  
  84. -- 
  85. -----------------------------------------
  86. Lawrence Kirby | fred@genesis.demon.co.uk
  87. Wilts, England | 70734.126@compuserve.com
  88. -----------------------------------------
  89.